<template>
  <Modal :mask-closable="false" v-model="create" width="700" :title="type=='new'?'标签打印':'标签补印'" class="modal-content-layout">
    <Form ref="form" class="formList" :model="formItem" label-position="top" :rules="ruleValidate">
      <Row :gutter="20">
        <Col span="12">
        <FormItem label="标签类型" prop="labelType">
          <Select :disabled="type == 'edit'" v-model="formItem.labelType" :placeholder="`${$t('common.selectInfo')}`">
            <Option v-for="item of labelList" :value="item.label" :key="item.value" :label="item.label">
            </Option>
          </Select>
        </FormItem>
        </Col>
        <Col span="12">
        <FormItem label="工单号" prop="workCode">
          <Input disabled type="text" v-model="formItem.workCode"></Input>
        </FormItem>
        </Col>
      </Row>
      <Row :gutter="20">
        <Col span="12">
        <FormItem label="产品编号" prop="materialCode">
          <Input disabled type="text" v-model="formItem.materialCode"></Input>
        </FormItem>
        </Col>
        <Col span="12">
        <FormItem label="产品名称" prop="materialName">
          <Input disabled type="text" v-model="formItem.materialName"></Input>
        </FormItem>
        </Col>
      </Row>
      <Row :gutter="20">
        <Col span="12">
        <FormItem label="产品型号" prop="materialModel">
          <Input disabled type="text" v-model="formItem.materialModel"></Input>
        </FormItem>
        </Col>
        <Col span="12">
        <FormItem label="装箱数量" prop="packingQuantity">
          <InputNumber 
              :disabled="type == 'edit'" 
              :active-change="false"
              :precision="3"
              :max='99999999.999'
              style="width:100%"
              :parser="(val)=>util.numberFormatNoFixed(val,3)"
              v-model="formItem.packingQuantity">
            </InputNumber>
        </FormItem>
        </Col>
      </Row>
      <Row :gutter="20">
        <Col span="12">
        <FormItem label="张数" prop="printQuantity">
          <InputNumber style="width:100%" :min="1" v-model="formItem.printQuantity"></InputNumber>
        </FormItem>
        </Col>
        <Col span="12">
        <FormItem label="工号" prop="empno">
          <Input disabled type="text" v-model="formItem.empno"></Input>
        </FormItem>
        </Col>
      </Row>
      <Row :gutter="20">
        <Col span="12">
        <FormItem label="制造日期" prop="shiftTime">
          <DatePicker type="date" :disabled="type == 'edit'" :value="formItem.shiftTime" @on-change="timeChange"
            :placeholder="$t('common.PleaseSelect')" transfer-class-name="hide-handle-cancel" placement="bottom" transfer
            :clearable="false">
          </DatePicker>
        </FormItem>
        </Col>
        <Col span="12">
        <FormItem label="客户" prop="custom">
          <Input disabled type="text" v-model="formItem.custom"></Input>
        </FormItem>
        </Col>
      </Row>
      <template v-if="formItem.labelType == '产品标签' || formItem.labelType == '不良品标签'">
        <Row :gutter="20">
          <Col span="12">
          <FormItem label="班次" prop="cla">
            <Select v-model="formItem.cla" filterable clearable :disabled="type == 'edit'"
              :placeholder="`${$t('common.selectInfo')}`">
              <Option v-for="item of shiftList" :value="item.shift" :key="item.id" :label="item.shift">
              </Option>
            </Select>
          </FormItem>
          </Col>
          <Col span="12" v-if="type == 'edit'">
          <FormItem label="批号" prop="batchNumber">
            <Input disabled type="text" v-model="formItem.batchNumber"></Input>
          </FormItem>
          </Col>
          <Col span="12">
          <FormItem label="材料牌号" prop="productGrade">
            <Input type="text" v-model="formItem.productGrade"></Input>
          </FormItem>
          </Col>
        </Row>
      </template>
      <template v-if="formItem.labelType == '老厂产品标签' || formItem.labelType == '退火标签'">
        <Row :gutter="20">
          <Col span="12">
          <FormItem label="内外模" prop="modelType" v-if="formItem.labelType == '老厂产品标签'">
            <Select :disabled="type == 'edit'" v-model="formItem.modelType" :placeholder="`${$t('common.selectInfo')}`">
              <Option v-for="item of modelTypeList" :value="item.value" :key="item.value" :label="item.label">
              </Option>
            </Select>
          </FormItem>
          <FormItem label="炉号" prop="heatNumber" v-else>
            <Select :disabled="type == 'edit'" v-model="formItem.heatNumber" :placeholder="`${$t('common.selectInfo')}`">
              <Option v-for="item of luhaoList" :value="item.value" :key="item.value" :label="item.label">
              </Option>
            </Select>
          </FormItem>
          </Col>
            <Col span="12">
          <FormItem label="材料牌号" prop="customMark">
            <Input type="text" v-model="formItem.customMark"></Input>
          </FormItem>
          </Col>
        </Row>
      </template>
      <template v-if="formItem.labelType == '条料标签'">
        <Row :gutter="20">
          <Col span="12">
          <FormItem label="料位" prop="materialLevel">
            <Input :disabled="type == 'edit'" type="text" v-model="formItem.materialLevel"></Input>
          </FormItem>
          </Col>
          <Col span="12">
          <FormItem label="班次" prop="shift">
            <Select v-model="formItem.shift" filterable clearable :disabled="type == 'edit'"
              :placeholder="`${$t('common.selectInfo')}`">
              <Option v-for="item of shiftList" :value="item.shift" :key="item.id" :label="item.shift">
              </Option>
            </Select>
          </FormItem>
          </Col>
        </Row>
        <Row :gutter="20" v-if="type == 'edit'">
          <Col span="12">
          <FormItem label="批号" prop="batchNumber">
            <Input disabled type="text" v-model="formItem.batchNumber"></Input>
          </FormItem>
          </Col>
        </Row>
      </template>
      <template v-if="formItem.labelType == '品管合格证'">
        <Row :gutter="20">
          <Col span="12">
          <FormItem label="合同编号" prop="contractNumber">
            <Input type="text" v-model="formItem.contractNumber"></Input>
          </FormItem>
          </Col>
          <Col span="12">
          <FormItem label="包装编号" prop="packagingNumber">
            <Input type="text" v-model="formItem.packagingNumber"></Input>
          </FormItem>
          </Col>
        </Row>
        <Row :gutter="20">
          <Col span="12">
          <FormItem label="检验" prop="test">
            <Input :disabled="type == 'edit'" type="text" v-model="formItem.test"></Input>
          </FormItem>
          </Col>
          <Col span="12">
          <FormItem label="制造日期" prop="shiftTime">
            <DatePicker type="date" :disabled="type == 'edit'" v-model="formItem.shiftTime" @on-change="timeChange"
              :placeholder="$t('common.PleaseSelect')" transfer-class-name="hide-handle-cancel" placement="bottom"
              transfer :clearable="false">
            </DatePicker>
          </FormItem>
          <!-- <FormItem  label="制造日期" prop="shiftTime">
              <DatePicker type="date" placeholder="Select date" v-model="formItem.shiftTime"></DatePicker>
          </FormItem> -->
          </Col>
        </Row>
        <Row :gutter="20">
          <Col :span="type == 'new' ? 24 : 12">
          <FormItem label="备注" prop="remarks">
            <Input type="text" v-model="formItem.remarks"></Input>
          </FormItem>
          </Col>
          <Col span="12" v-if="type == 'edit'">
          <FormItem label="批号" prop="batchNumber">
            <Input disabled type="text" v-model="formItem.batchNumber"></Input>
          </FormItem>
          </Col>
        </Row>
      </template>
    </Form>
    <div slot="footer">
      <Button type="default" @click="create = false">{{
        $t("common.Cancle")
      }}</Button>
      <Button type="primary" :loading="loading" @click="addOk('form')">{{
        $t("maintenance.Confirmed")
      }}</Button>
    </div>
  </Modal>
</template>

<script>
import util from "@/util/util";
export default {
  data() {
    return {
      ruleValidate: {
        labelType: [
          {
            required: true,
            message: "请选择打印类型",
            trigger: "blur",
          },
        ],
        printQuantity: [
          {
            required: true,
            type: "number",
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
        empno: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
        materialCode: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
        materialName: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
        materialLevel: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
        workCode: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
        packingQuantity: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "change",
            type:'number'
          },
        ],
        heatNumber: [
          {
            required: true,
            message:'请选择',
            trigger: "blur",
          },
        ],
        shiftTime: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
          // { required: true, type: 'date', message: 'Please select the date', trigger: 'change' }
        ],
        // shiftTime:         { required: true, type: 'string', message: 'Please select the date', trigger: 'change' },
        contractNumber: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
        packagingNumber: [
          {
            required: true,
            message: this.$t("common.inputInfo"),
            trigger: "blur",
          },
        ],
      },
      shiftList: [],
      create: false,
      formItem: {
        shiftTime: '',
      },
      labelList: [
        {
          label: "产品标签",
          value: 1,
        },
        {
          label: "条料标签",
          value: 2,
        },
        {
          label: "品管合格证",
          value: 3,
        },
        {
          label: "老厂产品标签",
          value: 4,
        },
        {
          label: "不良品标签",
          value: 5,
        },
        {
          label: "退火标签",
          value: 6,
        },
      ],
      modelTypeList: [
        {
          label: "内",
          value: 0,
        },
        {
          label: "外",
          value: 1,
        },
      ],
      type: "",
      loading: false,
    };
  },
  computed: {
    luhaoList() {
      let res = [];
      if (this.formItem.deviceNo) {
       const code=this.formItem.deviceNo;
       switch (code) {
        case 'I1':
          res=[{label:'1',value:'1'},{label:'2',value:'2'},{label:'3',value:'3'},{label:'4',value:'4'}]
          break;
        case 'I2':
          res=[{label:'5',value:'5'},{label:'6',value:'6'}]
          break;
        case 'I10':
          res=[{label:'7',value:'7'}]
          break;
       }
       return res;
      }else return res
    }
  },
  methods: {
    timeChange(val) {
      this.formItem = {
        ...this.formItem,
        shiftTime: val
      }
      this.getShiftList();
      if (!val) {
        return;
      }
    },
    async show(row, type) {
      this.$refs.form.resetFields();
      this.type = type;
      if (type == "new") {
        this.formItem = {
          ...row,
          printQuantity: 1,
          packingQuantity:1,
          labelType: "产品标签",
        };
      } else {
        this.formItem = {
          ...row,
        };
      }
      this.timeChange(this.formItem.shiftTime)
      await this.getOtherInfo();
      this.create = true;
    },
    async getOtherInfo() {
      let res = await this.axios({
        url:'/dms/pRoductionReporting/querySubsequentOrder',
        method:'get',
        params:{
          orderCode:this.formItem.orderCode
        }
      })
      if(res.code == 200){
        Object.assign(this.formItem,res.data)
      }
      return;
    },
    addOk(name) {
      this.$refs[name].validate(valid => {
        if (!valid) {
          return;
        } else {
          this.loading = false;
          this.$emit("print", this.formItem);
        }
      });
    },
    close() {
      this.create = false;
    },
    getShiftList() {
      this.axios({
        method: "get",
        url: "/dms/mobile/getShiftList",
        params: {
          shiftDate: this.formItem.shiftTime,
          deviceNo: this.formItem.deviceNo,
        },
      }).then((res) => {
        this.shiftList = res.data;
      });
    },
  },
};
</script>

